package com.bsj.media.init.websocket;

import org.yeauty.pojo.Session;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class SessionChannelUntil {

    //key为sim卡，value为对应的哪个IP
    private static Map<String, Set<Session>> channelAndIp = new ConcurrentHashMap<>();

    //key为ip和端口，对应的管道
    private static Map<Session, Set<String>> mapNetInfo = new ConcurrentHashMap();


    public static Set<Session> getChannelAndIp(String devId){
        return channelAndIp.get(devId);
    }

    public static void push(String simChannel, Session session) {
        Set<Session> sessions = channelAndIp.get(simChannel);
        if (sessions == null) {
            sessions = new HashSet<>();
        }
        sessions.add(session);
        channelAndIp.put(simChannel, sessions);
        Set<String> set = mapNetInfo.get(session);
        if (set == null) {
            set = new HashSet<>();
        }
        set.add(simChannel);
        mapNetInfo.put(session, set);
    }

//    //强行删除session下的所有东西
    public static void del(Session session) {
        Set<String> set = mapNetInfo.get(session);
        if (set != null && !set.isEmpty()) {
            for (String simChannel : set) {
                del(session, simChannel);
            }
        }
    }

    public static void del(Session session, String simChannel) {
        Set<Session> sessions = channelAndIp.get(simChannel);
        if (sessions != null) {
            sessions.remove(session);
        }
        if (sessions == null || sessions.isEmpty()) {
            channelAndIp.remove(simChannel);
        }
    }
}
